<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="前端登录界面验证码详解," />





  <link rel="alternate" href="/atom.xml" title="OBKoro1's Blog" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="写在前面：前段事件，做了一个用ajax后台异步交互的登录功能，自己在上面加了一个验证码的功能，这个功能背后的原理挺好理解的，实现起来也十分简单，特此写波分享，，自己写的过程中踩了不少坑，这里还是照例写的详细点，大家可以做个参考，喜欢的朋友可以点个赞，或者关注一波。 最终实现的效果： 验证码效果当点击登录之前，会先判断验证码是否正确（验证码可以不区分大小写，也可以区分大小写），验证码错误会刷新验证码">
<meta name="keywords" content="前端登录界面验证码详解">
<meta property="og:type" content="article">
<meta property="og:title" content="使用 jQuery, Angular.js 实现登录界面验证码详解">
<meta property="og:url" content="http://yoursite.com/2017/10/11/使用-jQuery-Angular-js-实现登录界面验证码详解/index.html">
<meta property="og:site_name" content="OBKoro1&#39;s Blog">
<meta property="og:description" content="写在前面：前段事件，做了一个用ajax后台异步交互的登录功能，自己在上面加了一个验证码的功能，这个功能背后的原理挺好理解的，实现起来也十分简单，特此写波分享，，自己写的过程中踩了不少坑，这里还是照例写的详细点，大家可以做个参考，喜欢的朋友可以点个赞，或者关注一波。 最终实现的效果： 验证码效果当点击登录之前，会先判断验证码是否正确（验证码可以不区分大小写，也可以区分大小写），验证码错误会刷新验证码">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://dn-mhke0kuv.qbox.me/c4f72f1e1ae450c6b3ef">
<meta property="og:updated_time" content="2019-06-15T06:19:47.644Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用 jQuery, Angular.js 实现登录界面验证码详解">
<meta name="twitter:description" content="写在前面：前段事件，做了一个用ajax后台异步交互的登录功能，自己在上面加了一个验证码的功能，这个功能背后的原理挺好理解的，实现起来也十分简单，特此写波分享，，自己写的过程中踩了不少坑，这里还是照例写的详细点，大家可以做个参考，喜欢的朋友可以点个赞，或者关注一波。 最终实现的效果： 验证码效果当点击登录之前，会先判断验证码是否正确（验证码可以不区分大小写，也可以区分大小写），验证码错误会刷新验证码">
<meta name="twitter:image" content="https://dn-mhke0kuv.qbox.me/c4f72f1e1ae450c6b3ef">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/10/11/使用-jQuery-Angular-js-实现登录界面验证码详解/"/>





  <title>使用 jQuery, Angular.js 实现登录界面验证码详解 | OBKoro1's Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?64d57d0b926bbd27431daa89d2e0a76b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">OBKoro1's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-前端算法">
          <a href="/arithmetic/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-book"></i> <br />
            
            前端算法
          </a>
        </li>
      
        
        <li class="menu-item menu-item-博客最新更新">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            博客最新更新
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/11/使用-jQuery-Angular-js-实现登录界面验证码详解/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OBKoro1">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/hand1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="OBKoro1's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">使用 jQuery, Angular.js 实现登录界面验证码详解</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-11T22:43:34+08:00">
                2017-10-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/jQuery/" itemprop="url" rel="index">
                    <span itemprop="name">jQuery</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2017/10/11/使用-jQuery-Angular-js-实现登录界面验证码详解/" class="leancloud_visitors" data-flag-title="使用 jQuery, Angular.js 实现登录界面验证码详解">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,354 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  10分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="写在前面："><a href="#写在前面：" class="headerlink" title="写在前面："></a>写在前面：</h2><p>前段事件，做了一个用ajax后台异步交互的登录功能，自己在上面加了一个验证码的功能，这个功能背后的原理挺好理解的，实现起来也十分简单，特此写波分享，，自己写的过程中踩了不少坑，这里还是照例写的详细点，大家可以做个参考，喜欢的朋友可以点个赞，或者关注一波。</p>
<h2 id="最终实现的效果："><a href="#最终实现的效果：" class="headerlink" title="最终实现的效果："></a>最终实现的效果：</h2><p><img src="https://dn-mhke0kuv.qbox.me/c4f72f1e1ae450c6b3ef" alt=""></p>
<h3 id="验证码效果"><a href="#验证码效果" class="headerlink" title="验证码效果"></a>验证码效果</h3><p>当点击登录之前，会先判断验证码是否正确（验证码可以不区分大小写，也可以区分大小写），验证码错误会刷新验证码，验证码验证之前，不会进行跨域登录操作。</p>
<h3 id="整体思路。"><a href="#整体思路。" class="headerlink" title="整体思路。"></a>整体思路。</h3><p>1.取四位随机数</p>
<p>2.赋值到验证码的input框里。</p>
<p>3.在点击登录之前先用if判断验证码input框的值和输入框的值是否相等，相等时进入下一步操作，不相等直接返回错误</p>
<p>4.里面ajax的部分可以直接套进去。</p>
<h3 id="细节："><a href="#细节：" class="headerlink" title="细节："></a>细节：</h3><p>1.这里的<strong>验证码框的背景图片</strong>是网上自己找的，显得验证码比较正式，不然显得有点low。</p>
<p>2.<strong>不区分大小写</strong>实际上就是利用js的toUpperCase()方法是把小写转换成大写，因为是原生js所以在angular中也可以使用！</p>
<p>3.将验证码封装成一个函数，然后在点击登录时在最后调用这个函数，可以<strong>每次都刷新函数</strong>。</p>
<p>4.<strong>避免验证码被复制</strong>，在html里面使用：disabled=”disabled”——禁止验证码框文字被选中。</p>
<h3 id="下面是代码部分实现过程详解（注释写的比较详细）："><a href="#下面是代码部分实现过程详解（注释写的比较详细）：" class="headerlink" title="下面是代码部分实现过程详解（注释写的比较详细）："></a>下面是代码部分实现过程详解（注释写的比较详细）：</h3><p>html代码应该不会解释了，有不懂的，可以在评论区问我。下面<strong>有部分关于angular的内容，暂时还没学到这里可以跳过去</strong>，没有影响到实现效果的。(可以把代码复制过去，然后在自己本地试试。)</p>
<p><strong>先放用jq实现的过程，然后放angular实现的过程</strong>，看过我几篇文章的都知道，我尽量会把所有代码，每一步都注释的清清楚楚，希望可以帮助到大家。</p>
<h3 id="这里是html的内容："><a href="#这里是html的内容：" class="headerlink" title="这里是html的内容："></a>这里是html的内容：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class=&quot;js5-form&quot; id=&quot;js5-form&quot; ng-controller=&quot;enterCtrl&quot;&gt;</span><br><span class="line">        &lt;div id=&quot;enter-all&quot; &gt;</span><br><span class="line">            &lt;h3&gt;jnshu后台登录&lt;/h3&gt;</span><br><span class="line">            &lt;form action=&quot;&quot; name=&quot;myForm&quot;&gt;</span><br><span class="line">                &lt;div class=&quot;js5-input-div&quot;&gt;</span><br><span class="line">                    &lt;div class=&quot;js5-input-img1&quot;&gt;&lt;/div&gt;</span><br><span class="line">                    &lt;input id=&quot;js5-userNum&quot; type=&quot;text&quot; name=&quot;userName&quot; placeholder=&quot;用户名&quot; maxlength=&quot;12&quot;  ng-model=&quot;userName&quot; ng-keyup=&quot;mykey($event)&quot; required/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">            &lt;form action=&quot;&quot; name=&quot;registerForm&quot;&gt;</span><br><span class="line">                &lt;div class=&quot;js5-input-div&quot;&gt;</span><br><span class="line">                    &lt;div class=&quot;js5-input-img2&quot;&gt;&lt;/div&gt;</span><br><span class="line">                    &lt;input id=&quot;js5-password&quot; type=&quot;password&quot; name=&quot;userPsd&quot; placeholder=&quot;密码&quot; maxlength=&quot;20&quot; ng-model=&quot;userPsd&quot; ng-keyup=&quot;mykey($event)&quot; ng-minlength=&quot;5&quot; ng-maxlength=&quot;16&quot; required/&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br><span class="line">            &lt;!--账号和密码的登录框--&gt;</span><br><span class="line">            &lt;form action=&quot;&quot; &gt;</span><br><span class="line">                &lt;div class=&quot;js5-input-div&quot;&gt;</span><br><span class="line">                    &lt;span class=&quot;js5-input-divSpan&quot;&gt;验证码：&lt;/span&gt;</span><br><span class="line">                    &lt;input type=&quot;text&quot; placeholder=&quot;不区分大小写&quot; class=&quot;js5-form3-input&quot; id=&quot;js5-form3-input&quot; ng-model=&quot;writeCode&quot;  maxlength=&quot;6&quot; ng-keyup=&quot;mykey($event)&quot;&gt;</span><br><span class="line">                    &lt;input type=&quot;text&quot; class=&quot;js5-authCode&quot; value=&quot;&quot;  id=&quot;js5-authCode&quot; ng-model=&quot;showAuthCode&quot; disabled=&quot;disabled&quot;&gt;</span><br><span class="line">                     &lt;!--disabled=&quot;disabled&quot;禁止验证码框文字被选中--&gt;</span><br><span class="line">                    &lt;span class=&quot;spanShift&quot; ng-click=&quot;changeVerify()&quot;&gt;获取&lt;/span&gt;</span><br><span class="line">                &lt;/div&gt;</span><br><span class="line">            &lt;/form&gt;</span><br></pre></td></tr></table></figure>
<h3 id="这里是jq代码实现部分："><a href="#这里是jq代码实现部分：" class="headerlink" title="这里是jq代码实现部分："></a>这里是jq代码实现部分：</h3><p>记得引入jq文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">var authCode;</span><br><span class="line">    randomCode=$(&quot;#js5-authCode&quot;).eq(0);//获取验证码出现的方框dom</span><br><span class="line">console.log(randomCode);</span><br><span class="line"></span><br><span class="line">function createCode() &#123;</span><br><span class="line">    authCode=&quot;&quot;;//设置这个为空变量，然后往里面添加随机数</span><br><span class="line">    var authCodeLength=4;//随机数的长度</span><br><span class="line">    randomArray=[0,1,2,3,4,5,6,7,8,9,&apos;A&apos;,&apos;B&apos;,&apos;C&apos;,&apos;D&apos;,&apos;E&apos;,&apos;F&apos;,&apos;G&apos;,&apos;H&apos;,&apos;I&apos;,&apos;J&apos;,&apos;K&apos;,&apos;L&apos;,&apos;M&apos;,&apos;N&apos;,&apos;O&apos;,&apos;P&apos;,&apos;Q&apos;,&apos;R&apos;, &apos;S&apos;,&apos;T&apos;,&apos;U&apos;,&apos;V&apos;,&apos;W&apos;,&apos;X&apos;,&apos;Y&apos;,&apos;Z&apos;];</span><br><span class="line">    //创建一个数组，随机数从里面选择四位数或者更多</span><br><span class="line">    for(var i=0;i&lt;authCodeLength;i++)&#123;</span><br><span class="line">        var index=Math.floor(Math.random()*36);//随机取一位数</span><br><span class="line">        authCode +=randomArray[index];//取四位数，并+相连</span><br><span class="line">    &#125;</span><br><span class="line">    console.log(authCode);//取到四位随机数之后，跳出循环</span><br><span class="line">    randomCode.val(authCode);//将四位随机数赋值给验证码出现的方框</span><br><span class="line">    console.log(randomCode.val());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//以上是封装的获取验证码的函数</span><br><span class="line"></span><br><span class="line">$(function () &#123;//当文档加载结束后，运行这个函数</span><br><span class="line">    createCode();//一开始先运行一遍取随机数的函数</span><br><span class="line">    $(&quot;#js5-btn&quot;).click(function () &#123;//这里是一个点击事件</span><br><span class="line">        console.log(window.randomCode);</span><br><span class="line">        //这里写了一个必报，window.randomCode是在文档里面找到这个dom，否则上文的四个随机数传不到这里来</span><br><span class="line">        var randomCode=window.randomCode.val();</span><br><span class="line">        console.log(randomCode);</span><br><span class="line">        var authInput=$(&quot;#js5-form3-input&quot;).val().toUpperCase(),</span><br><span class="line">            user=$(&quot;#js5-userNum&quot;).val(),</span><br><span class="line">            psd=$(&apos;#js5-password&apos;).val();</span><br><span class="line">        //上面三个是分别获取验证码输入框的值，账号的值，密码的值。</span><br><span class="line">        //验证码输入框这里，最后toUpperCase()方法是把小写转换成大写</span><br><span class="line">        console.log(authInput);</span><br><span class="line">        console.log(randomCode);</span><br><span class="line">        console.log(user,psd);</span><br><span class="line">        if (randomCode===authInput) &#123;</span><br><span class="line">        //验证验证码，在验证码输入框与验证码的值不相等之前，是不会进入下面登录的步骤的，验证码是第一步关卡</span><br><span class="line">            var firstAjax = new XMLHttpRequest();</span><br><span class="line">            //创建ajax对象，这里是ajax跨域的部分</span><br><span class="line">            firstAjax.open(&quot;POST&quot;, &quot;这里是你url跨域的地址&quot;, true);</span><br><span class="line">            //连接服务器，跨域。</span><br><span class="line">            firstAjax.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);</span><br><span class="line">            //setRequestHeader() 方法指定了一个 HTTP 请求的头部，它应该包含在通过后续 send() 调用而发布的请求中。</span><br><span class="line">            //可以理解为，这是http的请求头，固定格式，位置必须要在open之后，send之前。</span><br><span class="line">            firstAjax.send(&quot;name=&quot; + user + &quot;&amp;pwd=&quot; + psd);</span><br><span class="line">            //在使用POST方式时参数代表着向服务器发送的数据，前面两个是账号框和密码框</span><br><span class="line">            firstAjax.onreadystatechange = function () &#123;//当参数被传入服务器的时候，引用监听事件。</span><br><span class="line">                if (firstAjax.readyState == 4) &#123;//readyState四种状态，当执行四步完成之后</span><br><span class="line">                    if (firstAjax.status == 200) &#123;//返回的是200，代表成功，404未找到。</span><br><span class="line">                        var returnValue = JSON.parse(firstAjax.responseText);//取回由服务器返回的数据</span><br><span class="line">                        console.log(returnValue);</span><br><span class="line">                        if (returnValue.code == 0) &#123;//这里是后端定义的，当code==0的时候，代表登录正确。</span><br><span class="line">                            window.location.href = &quot;https://www.baidu.com/index.php?tn=98012088_3_dg&amp;ch=1&quot;;</span><br><span class="line">                            //后端返回的数据验证成功就跳转链接。</span><br><span class="line">                        &#125;</span><br><span class="line">                        else &#123;</span><br><span class="line">                            $(&quot;#js5Message&quot;).text(returnValue.message);//当code不等于0时，返回出错信息</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; else &#123;</span><br><span class="line">                        alert(&quot;出错咯，咯咯咯&quot;);//返回的不是200的时候，出错。</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;;</span><br><span class="line">            createCode();//点击登录按钮，验证之后会刷新验证码</span><br><span class="line">        &#125;</span><br><span class="line">        else &#123;</span><br><span class="line">            $(&quot;#js5Message&quot;).text(&quot;验证码错误，请重新输入&quot;);</span><br><span class="line">            createCode();//验证码错误，刷新验证码。</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="这是是angular代码实现部分："><a href="#这是是angular代码实现部分：" class="headerlink" title="这是是angular代码实现部分："></a>这是是angular代码实现部分：</h3><p>jq部分写的详细一点，这里也挺详细的，如果不懂的话，可以回头看看jq部分，原理都是一样的，复制到本地自己多试试，记得引angular文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">var enter=angular.module(&quot;myApp&quot;);</span><br><span class="line">enter.controller(&apos;enterCtrl&apos;,[&apos;$scope&apos;,&apos;$http&apos;,&apos;$state&apos;,function ($scope,$http,$state) &#123;</span><br><span class="line">    $scope.changeVerify=function () &#123;//定义了一个点击事件，获取验证码</span><br><span class="line">        var authCode=&quot;&quot;;</span><br><span class="line">        var authCodeLength=4;//取几个随机数字</span><br><span class="line">        var randomArray=[0,1,2,3,4,5,6,7,8,9,&apos;A&apos;,&apos;B&apos;,&apos;C&apos;,&apos;D&apos;,&apos;E&apos;,&apos;F&apos;,&apos;G&apos;,&apos;H&apos;,&apos;I&apos;,&apos;J&apos;,&apos;K&apos;,&apos;L&apos;,&apos;M&apos;,&apos;N&apos;,&apos;O&apos;,&apos;P&apos;,&apos;Q&apos;,&apos;R&apos;, &apos;S&apos;,&apos;T&apos;,&apos;U&apos;,&apos;V&apos;,&apos;W&apos;,&apos;X&apos;,&apos;Y&apos;,&apos;Z&apos;];</span><br><span class="line">        for(var i=0;i&lt;authCodeLength;i++)&#123;</span><br><span class="line">            var index=Math.floor(Math.random()*36);//随机取一位数</span><br><span class="line">            authCode +=randomArray[index];//取四位数，并+相连</span><br><span class="line">        &#125;</span><br><span class="line">        $scope.showAuthCode=authCode;//赋值</span><br><span class="line">        console.log($scope.showAuthCode);</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    //上面是封装的获取验证码的函数，会在下面进行调用</span><br><span class="line">  (function () &#123;</span><br><span class="line">        $scope.changeVerify();//调用点击事件。</span><br><span class="line">        $scope.enter=function (userName,userPsd) &#123;</span><br><span class="line">            //点击登录按钮事件，将双向绑定的账号密码当做参数传入函数</span><br><span class="line">            if ($scope.writeCode.toUpperCase() ==$scope.showAuthCode)&#123;//toUpperCase()将小写转化为大写</span><br><span class="line">                //双向绑定验证码输入框，可以直接使用，这里是验证验证码</span><br><span class="line">                $http(&#123;</span><br><span class="line">                    method:&quot;POST&quot;,</span><br><span class="line">                    url:&quot;你的跨域地址&quot;,//$http的固定格式</span><br><span class="line">                    params:&#123;</span><br><span class="line">                        &quot;name&quot;:userName,</span><br><span class="line">                        &quot;pwd&quot;:userPsd</span><br><span class="line">                        //双向绑定的参数传到下个页面</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;).then(function (res) &#123;</span><br><span class="line">                    //获取服务器返回的参数</span><br><span class="line">                    console.log(res);</span><br><span class="line">                    if (res.data.code!==0)&#123;</span><br><span class="line">                        //参数不为0的时候，弹出提示</span><br><span class="line">                        alert(res.data.message);</span><br><span class="line">                    &#125;else &#123;</span><br><span class="line">                        //参数为0的时候，跳转页面</span><br><span class="line">                        $state.go(&quot;home.studentList&quot;);</span><br><span class="line"></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;)</span><br><span class="line">            &#125;else &#123;</span><br><span class="line">                alert(&quot;验证码输入错误咯，咯咯咯&quot;);</span><br><span class="line">                $scope.changeVerify();//验证后，刷新验证码</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;());</span><br></pre></td></tr></table></figure>
<h2 id="后话"><a href="#后话" class="headerlink" title="后话"></a>后话</h2><p>断断续续写了两天，现在写的没有之前那么快了。。差不多就以上这些内容，有问题可在评论区留言。有不足欢迎指导，拍砖。</p>
<p><strong>最后</strong>：因为我经常看不懂别人写的分享，所以个人写文比较偏小白，写的不好之处，欢迎指点。然后就是希望看完的朋友点个<strong>喜欢</strong>，也可以<strong>关注</strong>一下我，现在这阶段基本上每个月都不会少于十五篇文章（看到干货我也会进行分享）。码字不易，感谢支持！<br><strong>ps</strong>：目前<strong>待业</strong>，坐标北京，求推荐工作。然后希望我写哪方面的文章可以在底下评论，或者是私信我，虽然写的不好，但我就当这是记录自己成长的一种方式咯。（前提是我会了，如果不会我也会记下来，等会了的时候再更出来。）<br><a href="http://www.jianshu.com/u/8d1dd8c80f06" target="_blank" rel="noopener"></a><strong><a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" rel="noopener">掘金个人主页</a>  ，</strong><a href="http://www.jianshu.com/u/8d1dd8c80f06" target="_blank" rel="noopener"><strong>简书主页链接</strong></a>，<strong><a href="http://blog.csdn.net/OBKoro1?skin=dark1" target="_blank" rel="noopener">csdn博客主页链接</a> ，<a href="https://github.com/OBKoro1" target="_blank" rel="noopener">github</a> 。</strong></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>听说，打赏我的人最后都找到了真爱。</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/pay/weixin.jpg" alt="OBKoro1 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/pay/ali.jpg" alt="OBKoro1 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/前端登录界面验证码详解/" rel="tag"><i class="fa fa-tag"></i> 前端登录界面验证码详解</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/10/11/sass变量详解——你不知道的sass。/" rel="next" title="sass变量详解——你不知道的sass。">
                <i class="fa fa-chevron-left"></i> sass变量详解——你不知道的sass。
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/11/http状态码是什么，有什么用，在哪里查看，分别代表什么意思？/" rel="prev" title="http状态码是什么，有什么用，在哪里查看，分别代表什么意思？">
                http状态码是什么，有什么用，在哪里查看，分别代表什么意思？ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          
            <img class="site-author-image" itemprop="image"
              src="/uploads/hand1.jpg"
              alt="OBKoro1" />
          
            <p class="site-author-name" itemprop="name">OBKoro1</p>
            <p class="site-description motion-element" itemprop="description">种一棵树最好的时间是十年前,其次是现在</p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="/archives">
            
                <span class="site-state-item-count">105</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">74</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/OBKoro1" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://juejin.im/user/58714f0eb123db4a2eb95372" target="_blank" title="掘金">
                  
                    <i class="fa fa-fw fa-globe"></i>掘金</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://obkoro1.com/web_accumulate/" target="_blank" title="前端积累">
                  
                    <i class="fa fa-fw fa-globe"></i>前端积累</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://segmentfault.com/u/obkoro1/articles" target="_blank" title="segmentfault">
                  
                    <i class="fa fa-fw fa-globe"></i>segmentfault</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#写在前面："><span class="nav-number">1.</span> <span class="nav-text">写在前面：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#最终实现的效果："><span class="nav-number">2.</span> <span class="nav-text">最终实现的效果：</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#验证码效果"><span class="nav-number">2.1.</span> <span class="nav-text">验证码效果</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#整体思路。"><span class="nav-number">2.2.</span> <span class="nav-text">整体思路。</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#细节："><span class="nav-number">2.3.</span> <span class="nav-text">细节：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#下面是代码部分实现过程详解（注释写的比较详细）："><span class="nav-number">2.4.</span> <span class="nav-text">下面是代码部分实现过程详解（注释写的比较详细）：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#这里是html的内容："><span class="nav-number">2.5.</span> <span class="nav-text">这里是html的内容：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#这里是jq代码实现部分："><span class="nav-number">2.6.</span> <span class="nav-text">这里是jq代码实现部分：</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#这是是angular代码实现部分："><span class="nav-number">2.7.</span> <span class="nav-text">这是是angular代码实现部分：</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#后话"><span class="nav-number">3.</span> <span class="nav-text">后话</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OBKoro1</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">
      156.0k
    </span>
  
</div>




<div>
<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span class="site-uv"><i class="fa fa-user"></i> 访问用户： <span class="busuanzi-value" id="busuanzi_value_site_uv">21621</span> 人 </span>
<span class="site-pv"><i class="fa fa-eye"></i> 访问次数： <span class="busuanzi-value" id="busuanzi_value_site_pv">68503</span> 次</span>

</div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>


  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  



  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("9ANFp60h6oVyxIPGlau3NOA4-gzGzoHsz", "tMdNuKMbFtN4QsekeNVu83TV");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>

